package com.example.administrator.demo_phonesearch;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.xmlpull.v1.XmlPullParser;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private EditText et1;
    private TextView tv1;
    private TextView tv2;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
                switch (msg.what){
                    case 1:
                        tv1.setText((String )msg.obj);
                        break;
                    case 2:
                        tv2.setText((String)msg.obj);
                        break;
                    case 3:
                        Toast.makeText(MainActivity.this,"失败了",0).show();
                        break;
                    case 4:
                        Toast.makeText(MainActivity.this,"url链接失败",0).show();
                }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et1=findViewById(R.id.et1);
        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);
    }
    public void search(View view){
        final String path=et1.getText().toString().trim();
        new Thread(){
            public void run(){
                try {
                    URL url=new URL("http://api.k780.com:88/?app=phone.get&phone="+path+"&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=xml");
                    HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("GET");
                    int code=conn.getResponseCode();
                    if (code!=200){
                        Message msg=Message.obtain();
                        msg.what=4;
                        handler.sendMessage(msg);
                    }
                    InputStream is=conn.getInputStream();
                    XmlPullParser xmlPullParser=Xml.newPullParser();
                    xmlPullParser.setInput(is,"utf-8");
                    int type=xmlPullParser.getEventType();
                    while (type!=xmlPullParser.END_DOCUMENT){
                        if (type==xmlPullParser.START_TAG){
                            if ("att".equals(xmlPullParser.getName())){
                                String att=xmlPullParser.nextText();
                                Message msg=Message.obtain();
                                msg.what=1;
                                msg.obj=att;
                                handler.sendMessage(msg);
                            }else if ("ctype".equals(xmlPullParser.getName())){
                                String ctype=xmlPullParser.nextText();
                                Message msg=Message.obtain();
                                msg.what=2;
                                msg.obj=ctype;
                                handler.sendMessage(msg);
                            }
                        }
                        type=xmlPullParser.next();
                    }
                    is.close();
                } catch (Exception e) {
                    Message msg=Message.obtain();
                    msg.what=3;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
